from aiogram import F
from aiogram.enums import ContentType
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.input import MessageInput, TextInput
from aiogram_dialog.widgets.kbd import Back, Button, Next, Select, Start
from aiogram_dialog.widgets.media import DynamicMedia
from aiogram_dialog.widgets.text import Format

from dc_shop.tg_bot.dialogs.extras import copy_start_data_to_ctx
from dc_shop.tg_bot.dialogs.getters.payments import payment_method_getter, payment_methods_getter
from dc_shop.tg_bot.dialogs.getters.users import user_info_getter
from dc_shop.tg_bot.dialogs.widgets import BetterScrollingGroup, LocaleText
from dc_shop.tg_bot.states import ClientSG, CreateOrderSG

from ...getters.dynamic_media import google_maps_asset_getter
from .handlers import (
    create_or_update_order,
    go_back_with_job_cancel,
    process_invoice_screenshot,
    set_self_delivery,
    set_user_contact_name,
    set_user_contact_phone,
    set_user_delivery_address,
)

enter_address_window = Window(
    DynamicMedia(
        "google_maps_video",
        when=F["google_maps_video"],
    ),
    LocaleText(
        "confirm-address-msg",
        delivery_address="{delivery_address}",
        when=F["delivery_address"],
    ),
    LocaleText("enter-address-msg", when=~F["delivery_address"]),
    Next(LocaleText("confirm-btn"), when=F["delivery_address"]),
    TextInput("delivery_address", str, on_success=set_user_delivery_address),
    Button(
        LocaleText("self-delivery-btn"),
        id="self_delivery_btn",
        on_click=set_self_delivery,
    ),
    Start(
        LocaleText("change-cart-btn"),
        id="back_to_menu",
        state=ClientSG.start,
    ),
    state=CreateOrderSG.enter_address,
    getter=(user_info_getter, google_maps_asset_getter),
)

enter_phone_window = Window(
    LocaleText("confirm-phone-msg", contact_phone="{contact_phone}", when=F["contact_phone"]),
    LocaleText("enter-phone-msg", when=~F["contact_phone"]),
    Next(LocaleText("confirm-btn"), when=F["contact_phone"]),
    TextInput("contact_phone", str, on_success=set_user_contact_phone),
    Back(LocaleText("back-btn")),
    state=CreateOrderSG.enter_phone,
    getter=user_info_getter,
)

enter_contact_name_window = Window(
    LocaleText("confirm-name-msg", contact_name="{contact_name}", when=F["contact_name"]),
    LocaleText("enter-name-msg", when=~F["contact_name"]),
    Next(LocaleText("confirm-btn"), when=F["contact_name"]),
    TextInput("contact_name", str, on_success=set_user_contact_name),
    Back(LocaleText("back-btn")),
    state=CreateOrderSG.enter_contact_name,
    getter=user_info_getter,
)


select_payment_method_window = Window(
    LocaleText("select-payment-method-msg"),
    BetterScrollingGroup(
        Select(
            Format("{item.currency}"),
            "payment_method_sel",
            lambda method: method.id,
            "payment_methods",
            on_click=create_or_update_order,
        ),
        width=2,
        height=3,
        id="useritemssel",
        hide_on_single_page=True,
    ),
    Back(LocaleText("back-btn")),
    state=CreateOrderSG.select_payment_method,
    getter=payment_methods_getter,
)

send_invoice_screenshot_window = Window(
    DynamicMedia("payment_image", when=F["payment_image"]),
    LocaleText(
        "send-invoice-screenshot-msg",
        summ="{dialog_data[order_summ]}",
        currency="{currency}",
        payment_address="{payment_address}",
    ),
    Format("{dialog_data[order_product_lines_text]}"),
    MessageInput(process_invoice_screenshot, content_types=ContentType.ANY),
    Button(
        LocaleText("back-btn"),
        id="back_with_job_cancel_btn",
        on_click=go_back_with_job_cancel,
    ),
    state=CreateOrderSG.send_invoice_screenshot,
    getter=payment_method_getter,
)

create_order_dialog = Dialog(
    enter_address_window,
    enter_phone_window,
    enter_contact_name_window,
    select_payment_method_window,
    send_invoice_screenshot_window,
    on_start=copy_start_data_to_ctx,
)
